package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Student;
import com.qf.entity.StudentVO;
import com.qf.service.StudentService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author cuicui
 * @Description
 * @Date 2025-8-15
 */
@RestController

public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 这里是根据id 查询学生信息接口
     */
    @GetMapping("student/{id}")
    public R findById(@PathVariable int id) {
        Student student = studentService.findById(id);
        if (student == null) {
            return R.fail(5000, "用户不存在");
        }
        return R.success(student);
    }

    /**
     * 查询全部学生信息
     */
    @GetMapping("student/all")
    public R findAllStudent() {//int pageSize,int pageNum
        // PageHelper.startPage(pageNum,pageSize);
        // List<Student> list = studentService.findAllStudent();
        // // 想要获取更多的分页信息,直接创建对象
        // PageInfo<Student> pageInfo = new PageInfo<>(list);
        // return R.success(pageInfo);
        List<Student> list = studentService.findAllStudent();
        return R.success(list);
    }

    /**
     * 模拟分页查询
     */
    @GetMapping("student/page")
    public R findAllStudent(int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = studentService.findAllStudent();
        // 想要获取更多的分页信息,直接创建对象
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        return R.success(pageInfo);
    }

    /**
     * 模拟搜索框根据年龄或者模糊搜索姓名查询学生
     */
    @GetMapping("student/search")
    public R findStudentByAgeOrName(String name, Integer age) {
        List<Student> list = studentService.findStudentByAgeOrName(name, age);
        return R.success(list);
    }

    /**
     * 新增学生信息
     */
    @PostMapping("student/add")
    public R addStudent(Student student) {
        int i = studentService.addStudent(student);
        if (i > 0) {
            return R.success("新增成功");
        }
        return R.fail(5000, "新增失败");
    }

    /**
     * 修改学生信息
     */
    @PostMapping("student/update")
    public R updateStudent(Student student) {
        int i = studentService.updateStudent(student);
        if (i > 0) {
            return R.success("修改成功");
        }
        return R.fail(5000, "修改失败");
    }

    /**
     * 删除学生信息
     */
    @GetMapping("student/delete")
    public R deleteStudent(int id) {
        int i = studentService.deleteStudent(id);
        if (i > 0) {
            return R.success("删除成功");
        }
        return R.fail(5000, "删除失败");
    }

    /**
     * 批量删除学生信息
     */

    @GetMapping("/student/delete/batch")
    public R deleteBatch(@RequestParam List<Integer> ids) {
        System.out.println("批量删除,接收到前端的id:" + ids);
        studentService.deleteBatch(ids);
        return R.success();
    }


    /**
     * 多表查询 查询所有学生信息以及关联的班级信息
     */
    @GetMapping("student/vo/all")
    public R findAllStudentVO() {
        List<StudentVO> list = studentService.findAllStudentVO();
        return R.success(list);
    }

    /**
     * 根据id查询1个学生信息以及关联的所有学科信息
     */
    @GetMapping("student/vo/findStudentAndAllSubjectByStuid/{sid}")
    public R findStudentVOById(@PathVariable int sid) {
        StudentVO studentVO = studentService.findStudentAndAllSubjectByStuid(sid);
        return R.success(studentVO);
    }
    // 根据学生id查出关联表中所关联的所有学科id
    @GetMapping("student/vo/findAllSubjectId")
    public R findAllSubjectId( int sid) {
        List<Integer> subIdList = studentService.findAllSubjectId(sid);
        return R.success(subIdList);
    }

    /**
     * 根据id查询1个学生信息以及关联的所有学科信息以及每个学科关联的老师信息
     */
    @GetMapping("student/vo/findStudentMsgByStuid/{sid}")
    public R findStudentMsgByStuid(@PathVariable int sid) {
        StudentVO studentVO = studentService.findStudentMsgByStuid(sid);
        return R.success(studentVO);
    }

    /**
     * 学生 --> 教室一对一查询,V2
     */
    @GetMapping("/class/v2")
    public R findAllStudentAndClassroomV2() {
        List<StudentVO> list = studentService.findAllStudentAndClassroomV2();
        return R.success(list);
    }

    /**
     * 业务层拆解一对多
     */
    @GetMapping("/subjects/v2")
    public R getSubjectById(int id) {
        StudentVO studentVO = studentService.getSubjectById(id);
        return R.success(studentVO);
    }

}
